package com.googlecode.jmicroerp.wicket.administrare;

import com.googlecode.jmicroerp.model.Produs;
import com.googlecode.jmicroerp.service.GenericService;
import com.googlecode.jmicroerp.service.ProduseService;
import com.googlecode.jmicroerp.wicket.DefaultEditPage;
import com.googlecode.jmicroerp.wicket.components.FileResource;
import com.googlecode.jmicroerp.wicket.components.NameValidator;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.lang.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author alex
 */
public class EditareProdusePage extends DefaultEditPage {
    private static final Logger LOGGER = LoggerFactory.getLogger(EditareProdusePage.class);
    private static final int MAX_SIZE = 1024;
    private Produs produs;
    private String caleImagine;
    @SpringBean(name = "genericService")
    private GenericService<Produs> genericService;
    private List<FileUpload> fileUpload;
    @SpringBean(name = "uploadPath")
    private String uploadPath;
    
    @SpringBean(name = "produseService")
    private ProduseService produseService;
 
    public EditareProdusePage() {
        produs = new Produs();
        produs.setActiv(Boolean.TRUE);
        caleImagine = "noimage.jpg";
        init();
    }

    public EditareProdusePage(Long id) {
        produs = genericService.getById(Produs.class, id);
        caleImagine = produs.getCaleImagine().toString();
        init();
    }

    private void init() {

        Form<Produs> form = new Form<Produs>("form");
        form.setModel(new CompoundPropertyModel<Produs>(produs));

    	final FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
        form.add(feedbackPanel);
        
        TextField<String> tfDenumire = new TextField<String>("denumire");
        tfDenumire.setRequired(true);
        
        if (produs.getId() == null) {
        	tfDenumire.add(new NameValidator(produseService));
        }
        
        form.add(tfDenumire);

        TextField<BigDecimal> tfPret = new TextField<BigDecimal>("pret");
        tfPret.setRequired(true);
        form.add(tfPret);
        
        Image img = new Image("caleImagine", new FileResource(uploadPath, caleImagine)) {
            @Override
            protected boolean shouldAddAntiCacheParameter() {
                return true;
            }
        };
        form.add(img);
        
        final FileUploadField fileUploadField = new FileUploadField("upload", new PropertyModel(this, "fileUpload"));
        form.setMaxSize(Bytes.kilobytes(MAX_SIZE));
        form.add(fileUploadField);

        
        CheckBox ckActiv = new CheckBox("activ");
        form.add(ckActiv);

        form.add(new SubmitLink("submit") {
            @Override
            public void onSubmit() {
                try {
                    if (fileUpload != null && fileUpload.get(0) != null) {
                        File newFile = new File(uploadPath, fileUpload.get(0).getClientFileName());
                        newFile.createNewFile();
                        fileUpload.get(0).writeTo(newFile);
                        produs.setCaleImagine(fileUpload.get(0).getClientFileName());
                    }
                } catch (IOException ioex) {
                    LOGGER.error("Eroare io", ioex);
                }
                if (produs.getId() == null) {
                    genericService.insert(produs);
                } else {
                    genericService.update(produs);
                }
                setResponsePage(VizualizareProdusePage.class);
            }
        });

        form.add(new Link("renuntare") {
            @Override
            public void onClick() {
                setResponsePage(VizualizareProdusePage.class);
            }
        });

        add(form);
    }
}
